<?php
namespace Home\Controller;

use Common\Controller\BaseController;

class CarController extends BaseController
{
    public $action = 0;
    public $page_size = 10;

    public function _initialize()
    {
        parent::_initialize();
        self::$_action_block = 1;
        $this->assign('block', self::$_action_block);
    }

    public function index()  //对应view中的index页面
    {
        vendor('Page');
        $this->action = 2;    //index页面中的action值为2
        $carModel = D('Car');

        $where = " staff.staff_id={$this->staff_id} and car.is_drift=1";

        $count = $carModel
            ->join('staff on staff.staff_id=car.staff_id', 'LEFT')
            ->where($where)
            ->count();        //获取记录数

        $Page = new \Vendor\Page($count, $this->page_size);
        $field = "car.*,staff.staff_name ";
        $list = $carModel->field($field)
            ->join('staff on staff.staff_id=car.staff_id', 'LEFT')
            ->where($where)
            ->order('apply_time DESC')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('list', $list);
        $this->assign('page', $Page->show());
        $this->assign('type', $this->action);
        $this->display('index');   //显示index页面
    }


    public function add()    //对应view中的add 3个页面
    {
        $this->action = 1;
        $step = !empty($_GET['step']) && (intval($_GET['step']) > 1) ? intval($_GET['step']) : 1;
        $staff = $this->getUserInfo();
        $this->assign('staff', $staff);
        $this->assign('type', $this->action);
        $this->display('add' . $step);
    }

    public function save($drift = 0)
    {
        $post = I('post.');
        $carModel = D('Car');
        if (!$post['is_agree']) {
            $this->error('请同意协议');
        }

        $image_path = $this->fileUpload();
        $post['is_drift'] = $drift ? 2 : 1;
        if (!$image_path['status']) {
            $this->error($image_path['info']);
        }
        $post ['car_photo'] = $image_path['info']['car_photo'];
        $post ['xingshi_license'] = $image_path['info']['xingshi_license'];
        $post ['jiashi_license'] = $image_path['info']['jiashi_license'];
//        dump($post);exit;
        $res = $carModel->addCar($post);
        if (false !== $res['status']) {
            $url = $drift ? U('Home/Car/drift') : U('Home/Car/index');
            $this->success($res['info'], $url);
        } else {
            $this->error($res['info']);
        }
        exit;
    }

    public function drift()   //草稿箱操作
    {
        vendor('Page');
        $carModel = D('Car');
        $this->action = 3;
        $where = " staff.staff_id={$this->staff_id} and car.is_drift=2";

        $count = $carModel
            ->join('staff on staff.staff_id=car.staff_id', 'LEFT')
            ->where($where)
            ->count();        //获取记录数

        $Page = new \Vendor\Page($count, $this->page_size);
        $field = "car.*,staff.staff_name ";
        $list = $carModel->field($field)
            ->join('staff on staff.staff_id=car.staff_id', 'LEFT')
            ->where($where)
            ->order('apply_time DESC')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('list', $list);
        $this->assign('page', $Page->show());
        $this->assign('type', $this->action);
        $this->display('drift');
    }

    public function edit()
    {
        $id = I('get.id', 0, 'intval');
        $where = "car.id={$id} and staff.staff_id={$this->staff_id} and car.is_drift=2";
        $carModel = D('Car');
        $field = " car.*,staff.*";
        $info = $carModel
            ->field($field)
            ->join('staff on car.staff_id=staff.staff_id')
            ->where($where)
            ->select();

        if (empty($info)) {
            $this->error('数据不存在');
        }
        $staff = $this->getUserInfo();
        $this->assign('staff', $staff);
        $this->assign('info', $info[0]);
        $this->display('edit');
    }

    public function delete()
    {
        $this->display('delete');
    }

    public function getUserInfo()
    {
        $staff_id = $this->staff_id;
        // $field = "";
        $staffModel = D('Staff');
        $staff = $staffModel->getStaffById($staff_id);
        return $staff;
    }

    public function view()
    {
        $id = I('get.id', 0, 'intval');
        $type = I('get.type', 0, 'intval');
        $where = "car.id={$id} and staff.staff_id={$this->staff_id} and status={$type} ";
        $carModel = D('Car');
        $field = " car.*,staff.*";
        $info = $carModel
            ->field($field)
            ->join('staff on car.staff_id=staff.staff_id')
            ->where($where)
            ->select();

        if (empty($info)) {
            $this->error('数据不存在');
        }
        $this->assign('info', $info[0]);
        $view = 'view_' . $type;
        $this->display($view);
    }

    public function fileUpload()
    {
        $return = array('status' => '', 'info' => '');
        $path = C('FILE_UPLOAD_PATH');
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 31457282;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = $path; // 设置附件上传根目录
//        $upload->savePath  =     date('Ymd'); // 设置附件上传（子）目录
        // 上传文件
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }

        $info = $upload->upload();

        if (!$info) {// 上传错误提示错误信息
            $return['status'] = false;
            $return['info'] = $upload->getError();
        } else {// 上传成功
            $return['status'] = true;
            $return['info']['car_photo'] = $info[0]['savepath'] . $info[0]['savename'];
            $return['info']['xingshi_license'] = $info[1]['savepath'] . $info[1]['savename'];
            $return['info']['jiashi_license'] = $info[2]['savepath'] . $info[2]['savename'];
        }
        return $return;
    }

}